/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include/upanddowndialog.h"
#include "ui_upanddowndialog.h"

#include <QProgressDialog>
#include <QMessageBox>
#include <QDebug>

#include "seuil/methode/upanddown.h"

UpAndDownDialog::UpAndDownDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UpAndDownDialog),
    attente_(new QProgressDialog)
{
    ui->setupUi(this);
    attente_->setModal(true);
    attente_->setWindowTitle(tr("Stimulation"));
    attente_->setLabelText(tr("Stimulation en cours..."));
    attente_->setMaximum(0);
    attente_->setValue(-1);

    connect(attente_, SIGNAL(accepted()), this, SLOT(demanderDouleur()));
    connect(ui->stimulerPushButton, SIGNAL(clicked()), this, SIGNAL(stimuler()));
}

UpAndDownDialog::~UpAndDownDialog()
{
    delete ui;
}

void UpAndDownDialog::changementPhase(int phase)
{
    using namespace seuil::methode;
    qDebug() << "UpAndDown dlg: Changement de phase (" << phase << ')';

    switch (phase) {
        case UpAndDown::Up :
            ui->upImageLabel->setPixmap(QPixmap(":/images/attente"));
            ui->confirmerImageLabel->setPixmap(QPixmap(":/images/attente"));
            ui->downImageLabel->setPixmap(QPixmap(":/images/attente"));
            ui->confirmerImageLabel->setEnabled(false);
            ui->confirmerLabel->setEnabled(false);
            ui->downImageLabel->setEnabled(false);
            ui->downLabel->setEnabled(false);
            break;

        case UpAndDown::Confirmation :
            ui->upImageLabel->setPixmap(QPixmap(":/images/fait"));
            ui->upLabel->setEnabled(false);
            ui->confirmerImageLabel->setEnabled(true);
            ui->confirmerLabel->setEnabled(true);
            break;

        case UpAndDown::Down :
            ui->confirmerImageLabel->setPixmap(QPixmap(":/images/fait"));
            ui->confirmerLabel->setEnabled(false);
            ui->downImageLabel->setEnabled(true);
            ui->downLabel->setEnabled(true);
            break;
    }
}

void UpAndDownDialog::terminer(bool ok)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
    if ( ok ) {
        ui->downImageLabel->setPixmap(QPixmap(":/images/fait"));
    }
}

void UpAndDownDialog::erreur(const QString &message)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    QPixmap imageErreur(":/images/erreur");

    if ( message == "confirmation" ) {
        ui->confirmerImageLabel->setPixmap(imageErreur);
        ui->downImageLabel->setPixmap(imageErreur);

    } else if ( message.startsWith("Up") ) {
        ui->upImageLabel->setPixmap(imageErreur);
        ui->confirmerImageLabel->setPixmap(imageErreur);
        ui->downImageLabel->setPixmap(imageErreur);

    } else if ( message.startsWith("Down") ) {
        ui->downImageLabel->setPixmap(imageErreur);
    }
}

void UpAndDownDialog::demanderDouleur()
{
    QMessageBox::StandardButton douleur =
            QMessageBox::question(0, tr("Douleur"), tr("Y a-t-il eu douleur ?"),
                                  QMessageBox::Yes | QMessageBox::No);

    emit douleurRecue(douleur == QMessageBox::Yes);
}

void UpAndDownDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
